import os
import platform


def get_cpu_info():
    system = platform.system().lower()

    if system == "windows":
        # 在 Windows 上通过命令行获取 CPU 信息
        try:
            result = os.popen('wmic cpu get name').read().strip().split('\n')
            cpu_name = result[-1].strip() if len(result) > 1 else "Unknown"
            return f"CPU: {cpu_name}"
        except Exception as e:
            return f"Error retrieving CPU info on Windows: {e}"

    elif system == "linux":
        # 在 Linux 上通过读取 /proc/cpuinfo 文件获取 CPU 信息
        try:
            with open('/proc/cpuinfo', 'r') as f:
                lines = f.readlines()

            # 尝试从多个字段中提取 CPU 信息
            possible_fields = ['model name', 'processor', 'cpu model', 'Hardware']
            for field in possible_fields:
                for line in lines:
                    if field in line.lower():
                        cpu_info = line.split(":", 1)[1].strip()
                        return f"CPU: {cpu_info}"

            return "CPU information not found in /proc/cpuinfo"

        except Exception as e:
            return f"Error retrieving CPU info on Linux: {e}"

    else:
        return "Unsupported operating system"


def get_memory_info():
    system = platform.system().lower()

    if system == "windows":
        # 在 Windows 上通过 wmic 命令获取内存大小
        try:
            result = os.popen('wmic computersystem get TotalPhysicalMemory').read().strip().split('\n')
            memory_size = int(result[-1].strip()) // (1024 ** 3)  # 转换为 GB
            return f"内存: {memory_size} GB"
        except Exception as e:
            return f"Error retrieving memory info on Windows: {e}"

    elif system == "linux":
        # 在 Linux 上通过读取 /proc/meminfo 文件获取内存大小
        try:
            with open('/proc/meminfo', 'r') as f:
                for line in f:
                    if line.startswith('MemTotal'):
                        mem_total = int(line.split(':')[1].strip().split()[0]) // 1024  # 转换为 MB
                        return f"内存: {mem_total // 1024} GB ({mem_total} MB)"
            return "Memory information not found in /proc/meminfo"

        except Exception as e:
            return f"Error retrieving memory info on Linux: {e}"

    else:
        return "Unsupported operating system"


def get_os_info():
    system = platform.system().lower()

    if system == "windows":
        # 在 Windows 上通过 platform 模块获取操作系统名称和版本
        try:
            os_name = platform.system()
            os_version = platform.version()
            os_release = platform.release()
            return f"操作系统: {os_name} {os_release} (Version: {os_version})"
        except Exception as e:
            return f"Error retrieving OS info on Windows: {e}"

    elif system == "linux":
        # 在 Linux 上通过读取 /etc/os-release 文件获取操作系统名称和版本
        try:
            with open('/etc/os-release', 'r') as f:
                os_info = {}
                for line in f:
                    if '=' in line:
                        key, value = line.strip().split('=', 1)
                        os_info[key] = value.strip('"')

                # 获取操作系统名称和版本
                name = os_info.get('NAME', 'Unknown')
                version = os_info.get('VERSION', 'Unknown')
                id_like = os_info.get('ID_LIKE', '').lower()

                # 特殊处理国产操作系统
                if 'uos' in id_like or 'uniontech' in os_info.get('ID', '').lower():
                    name = "UnionTech OS (统信)"
                elif 'kylin' in id_like or 'kylin' in os_info.get('ID', '').lower():
                    name = "Kylin OS (麒麟)"

                return f"操作系统: {name} {version}"

        except Exception as e:
            return f"Error retrieving OS info on Linux: {e}"

    else:
        return "Unsupported operating system"


if __name__ == "__main__":
    print(get_cpu_info())
    print(get_memory_info())
    print(get_os_info())